/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI.SavingBookManager;
import BUS.CustomerBUS;
import DTO.Customer_DTO;
import java.awt.Point;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.SortOrder;

/**
 *
 * @author Nguyen Phi Hung
 */
public class CustomerManager extends javax.swing.JInternalFrame {
    TableCustomer model=new TableCustomer();
    CustomerBUS customer_bus=new CustomerBUS();
    String sql="select * from customer";
    public CustomerManager() throws Exception {
        initComponents();
        setOpaque(false);
        setResizable(false);
        setMaximum(false);
        model.SetTable(Table);
        model.SetData(sql);
                
       // LoadTableData();
       // this.load_data(0, 0);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane2 = new javax.swing.JScrollPane();
        Table = new org.jdesktop.swingx.JXTable();
        jPanel3 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        MaKH = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        HoTen = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        CMND = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        Email = new javax.swing.JTextField();
        DiaChi = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        SoDienThoai = new javax.swing.JTextField();
        NhapLai = new javax.swing.JButton();
        ThemKH = new javax.swing.JButton();
        XoaKH = new javax.swing.JButton();
        CapNhatKH = new javax.swing.JButton();

        setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        setResizable(true);
        setTitle("Quản Lý Thông Tin Khách Hàng");
        setFont(new java.awt.Font("Times New Roman", 0, 13)); // NOI18N

        Table.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        Table.setEditable(false);
        Table.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                TableMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(Table);

        jLabel1.setFont(new java.awt.Font("Times New Roman", 0, 13)); // NOI18N
        jLabel1.setText("Mã Sổ");

        jLabel2.setFont(new java.awt.Font("Times New Roman", 0, 13)); // NOI18N
        jLabel2.setText("Họ Tên");

        jLabel3.setFont(new java.awt.Font("Times New Roman", 0, 13)); // NOI18N
        jLabel3.setText("Số CMND");

        jLabel4.setFont(new java.awt.Font("Times New Roman", 0, 13)); // NOI18N
        jLabel4.setText("Địa /chỉ");

        jLabel5.setFont(new java.awt.Font("Times New Roman", 0, 13)); // NOI18N
        jLabel5.setText("Email");

        jLabel6.setFont(new java.awt.Font("Times New Roman", 0, 13)); // NOI18N
        jLabel6.setText("Số Điện Thoại");

        NhapLai.setFont(new java.awt.Font("Times New Roman", 0, 13)); // NOI18N
        NhapLai.setText("Nhập Lại");
        NhapLai.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                NhapLaiActionPerformed(evt);
            }
        });

        ThemKH.setFont(new java.awt.Font("Times New Roman", 0, 13)); // NOI18N
        ThemKH.setText("Thêm KH");
        ThemKH.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ThemKHActionPerformed(evt);
            }
        });

        XoaKH.setFont(new java.awt.Font("Times New Roman", 0, 13)); // NOI18N
        XoaKH.setText("Xóa KH");

        CapNhatKH.setFont(new java.awt.Font("Times New Roman", 0, 13)); // NOI18N
        CapNhatKH.setText("Cập Nhật KH");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(35, 35, 35)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3))
                .addGap(18, 18, 18)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(MaKH, javax.swing.GroupLayout.DEFAULT_SIZE, 160, Short.MAX_VALUE)
                    .addComponent(HoTen)
                    .addComponent(CMND))
                .addGap(52, 52, 52)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5)
                    .addComponent(jLabel6))
                .addGap(18, 18, 18)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(DiaChi, javax.swing.GroupLayout.DEFAULT_SIZE, 160, Short.MAX_VALUE)
                    .addComponent(Email)
                    .addComponent(SoDienThoai))
                .addGap(38, 38, 38)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(NhapLai)
                    .addComponent(XoaKH))
                .addGap(26, 26, 26)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(CapNhatKH, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(ThemKH, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(66, 66, 66))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(33, 33, 33)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(DiaChi, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(NhapLai, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(ThemKH, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel3Layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel5)
                                    .addComponent(Email, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(jPanel3Layout.createSequentialGroup()
                                .addGap(21, 21, 21)
                                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(XoaKH, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(CapNhatKH, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel6)
                            .addComponent(SoDienThoai, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(MaKH, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(HoTen, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(CMND, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 1021, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGap(46, 46, 46)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 508, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    /**
     * show data by row and column
     * @param row
     * @param col 
     */
    /*
    public void load_data(int row, int col){
        table_SavingbookModel mymodel = new table_SavingbookModel();
        
        if(row >= 0 && row <= Table.getRowCount()){
            mymodel.setRows(row);
            mymodel.setColumns(col);
            mymodel.setTable(Table);
            
            Vector vtdata = new Vector();
            vtdata = mymodel.getValueAtRow(row);
            
            //load info into textfield
            this.txt_MaSo_info.setText(vtdata.get(0).toString());
            this.txt_ChuSo_info.setText(vtdata.get(1).toString());
            this.txt_SoCMND_info.setText(vtdata.get(2).toString());
            this.txt_DiaChi_info.setText(vtdata.get(3).toString());
            //load info into Combobox
            try {
                this.ComboxBox_LTK_info.removeAllItems();
                mymodel.loadItemIntoComboBox(ComboxBox_LTK_info, row);
            } catch (Exception ex) {
                Logger.getLogger(CustomerManager.class.getName()).log(Level.SEVERE, null, ex);
            }
            //load OpeningDate into JdatePicker
            try {
                this.DatePicker_info.setDate(mymodel.getDatefromDatabase(row));
            } catch (ParseException ex) {
                Logger.getLogger(CustomerManager.class.getName()).log(Level.SEVERE, null, ex);
            }
            this.txt_SoTienDu_info.setText(vtdata.get(6).toString());
        }        
    }
    */
    /**
     * event mouse click table
     * @param evt 
     */
    private void TableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_TableMouseClicked
        //set row and column
        int row = Table.rowAtPoint(new Point(evt.getX(), evt.getY()));
        int col = Table.columnAtPoint(new Point(evt.getX(), evt.getY()));
        int index=Table.getSelectedRow();
        MaKH.setText(Table.getValueAt(index,0)+"");
        HoTen.setText(Table.getValueAt(index,1)+"");
        CMND.setText(Table.getValueAt(index,2)+"");
        DiaChi.setText(Table.getValueAt(index,3)+"");
        Email.setText(Table.getValueAt(index,4)+"");
        SoDienThoai.setText(Table.getValueAt(index,5)+"");
    }//GEN-LAST:event_TableMouseClicked

    private void NhapLaiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_NhapLaiActionPerformed
        // TODO add your handling code here:
        MaKH.setText("");
        HoTen.setText("");
        CMND.setText("");
        DiaChi.setText("");
        Email.setText("");
        SoDienThoai.setText("");
    }//GEN-LAST:event_NhapLaiActionPerformed

    private void ThemKHActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ThemKHActionPerformed
        // TODO add your handling code here:
        Customer_DTO info=new Customer_DTO();
        info.setCustomerID("");
        info.setFullName(HoTen.getText().trim());
        info.setIDNumber(CMND.getText().trim());
        info.setHomeAddress(DiaChi.getText().trim());
        info.setEmail(Email.getText().trim());
        info.setPhone(SoDienThoai.getText().trim());
        try {
            customer_bus.Insert("{call dbo.Insert_Customer(?,?,?,?,?,?)}", info);
        } catch (Exception ex) {
            Logger.getLogger(CustomerManager.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_ThemKHActionPerformed

    /**
     * 
     * @param evt 
     */
    public void LoadTableData() throws Exception{    
       //this.Table.setModel(new table_SavingbookModel().loadTable()); 
       this.Table.setSortOrder(0, SortOrder.ASCENDING);       
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField CMND;
    private javax.swing.JButton CapNhatKH;
    private org.jdesktop.swingx.JXDatePicker DatePicker_seach;
    private javax.swing.JTextField DiaChi;
    private javax.swing.JTextField Email;
    private javax.swing.JTextField HoTen;
    private javax.swing.JTextField MaKH;
    private javax.swing.JButton NhapLai;
    private javax.swing.JTextField SoDienThoai;
    private org.jdesktop.swingx.JXTable Table;
    private javax.swing.JButton ThemKH;
    private javax.swing.JButton XoaKH;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextField txt_ChuSo_seach;
    private javax.swing.JTextField txt_DiaChi_seach;
    private javax.swing.JTextField txt_DiaChi_seach1;
    private javax.swing.JTextField txt_MaSo_seach;
    private javax.swing.JTextField txt_SoCMND_seach;
    // End of variables declaration//GEN-END:variables
}
